package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/17 21:06
 */
@Service
@SuppressWarnings("all")
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    DtsCategoryMapper dtsCategoryMapper;

    @Override
    public List<CatVo> findList() {
//        先查询一级，在嵌套查询二级
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getLevel,"L1");
        //获取所有的一级分类
        List<DtsCategory> L1 = dtsCategoryMapper.selectList(wrapper);

        List<CatVo> catVoList = L1.stream().map(l1 -> {
            //将查询到的一级中的数据赋值给catvo
            CatVo catVo = new CatVo();

            catVo.setValue(l1.getId());
            catVo.setLabel(l1.getName());

            //设置二级中的数据
            LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DtsCategory::getDeleted, false);
            wrapper1.eq(DtsCategory::getLevel, "L2");
            //一级分类对应的二级分类
            wrapper1.eq(DtsCategory::getPid,l1.getId());

            //转换类型，将L2中的数据赋值给L1中的Children
            List<DtsCategory> L2 = dtsCategoryMapper.selectList(wrapper1);
            List<CatVo> catVos1 = L2.stream().map(l2 -> {

                CatVo catVo1 = new CatVo();
                catVo1.setValue(l2.getId());
                catVo1.setLabel(l2.getName());
                return catVo1;

            }).collect(Collectors.toList());

            catVo.setChildren(catVos1);
            return catVo;

        }).collect(Collectors.toList());

        return catVoList;
    }

    @Override
    public DtsCategory findById(Integer categoryId) {
        DtsCategory category = dtsCategoryMapper.selectById(categoryId);
        return category;
    }

    @Override
    public List<Integer> findByGoodsId(Integer CategoryId) {
        List<Integer> list = new ArrayList<>();
        //是主键，所以可以使用ID查询
        DtsCategory dtsCategory = dtsCategoryMapper.selectById(CategoryId);

        list.add(dtsCategory.getPid());
        list.add(CategoryId);

        return list;
    }

    /**
     * 商品类目分页搜索
     */
    @Override
    public Map<String, Object> findByL2Page(Integer page, Integer limit, String order, String sort, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
        if(id!=null){
            wrapper.eq("id", id);
        }
        if(name!=null){
            wrapper.like("name",name);
        }
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted",false );

        dtsCategoryMapper.selectPage(iPage,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());

        List<DtsCategory> items = new ArrayList<>();
        List<DtsCategory> records = iPage.getRecords();
        for (DtsCategory record : records) {
            items.add(record);
        }
        map.put("items",items);

        return map;
    }

    /**
     * 回显一级目录
     */

    @Override
    public List<Map<String, Object>> findByL1List() {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getLevel,"L1");

        List<DtsCategory> categories = dtsCategoryMapper.selectList(wrapper);

        List<Map<String, Object>> collect = categories.stream().map(dtsCategory -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsCategory.getId());
            map.put("label", dtsCategory.getName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 商品类目添加
     */
    @Override
    public void insert(DtsCategory dtsCategory) {
        //判断如果选择的是一级 那么Pid为0，前端传的Pid为Id
        if("L1".equalsIgnoreCase(dtsCategory.getLevel())){
            dtsCategory.setId(dtsCategory.getPid());
            dtsCategory.setPid(0);
            dtsCategory.setAddTime(new Date());
            dtsCategory.setUpdateTime(new Date());
            dtsCategory.setDeleted(false);
            dtsCategoryMapper.insert(dtsCategory);
        }
        if("L2".equalsIgnoreCase(dtsCategory.getLevel())){
            dtsCategory.setAddTime(new Date());
            dtsCategory.setUpdateTime(new Date());
            dtsCategory.setDeleted(false);
            dtsCategoryMapper.insert(dtsCategory);
        }
    }

    /**
     * 商品类目更新
     */
    @Override
    public void update(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryMapper.updateById(dtsCategory);
    }

    /**
     * 逻辑删除
     */
    @Override
    public void delete(DtsCategory dtsCategory) {
        dtsCategory.setDeleted(true);
        dtsCategoryMapper.updateById(dtsCategory);
    }
}
